using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Liberty.Application.GrantManagement;
using Liberty.Domain.ApplicationManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.GrantManagement;

using GenericSorter = Peradigm.Framework.Domain.Collections.Generic;
using Peradigm.Framework.Domain.Collections;
using Peradigm.Framework.Configuration;
using Peradigm.Framework.UI.Web;

namespace Liberty.UI.Service.GrantManagement.Controls
{
    public partial class GrantSummaryView : AbstractLibertyControl
    {
        #region Constants
        private const string
            TOTAL_AMOUNT_FORMAT = "Total Amount: {0:c}",
            SESSION_SORT_DESCRIPTION = "GrantSummarySortDescription",
            DEFAULT_SORT_PROPERTY = "Applicant.Name",
            COMMAND_SELECT_GRANT = "SelectGrant";

        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
            if( !this.IsPostBack )
            {
                this.SortDescription = new SortDescription( DEFAULT_SORT_PROPERTY );
            }
        }

        protected void cmdAddGrant_Click(object sender, EventArgs e)
        {
            this.RaiseAddNewGrantEvent( Case );
        }

        protected void gvGrantList_Sorting( object sender, GridViewSortEventArgs e )
        {
            this.SortDescription.Property = e.SortExpression;
        }
        protected void gvGrantList_RowCommand( object sender, GridViewCommandEventArgs e )
        {
            long oid;
            switch ( e.CommandName )
            {
                case COMMAND_SELECT_GRANT:
                    oid = long.Parse( e.CommandArgument.ToString() );
                    this.RaiseGrantSelectedEvent( this.GrantWith( oid ) );
                    break;
                default:
                    break;
            }
        }
        protected void gvGrantList_PageIndexChanging( object sender, GridViewPageEventArgs e )
        {
            gvGrantList.PageIndex = e.NewPageIndex;
        }
        #endregion

        #region Methods
        public override void UpdateView()
        {
            base.UpdateView();
            gvGrantList.DataSource = this.SortedGrantList();
            gvGrantList.DataBind();
            if( this.HasGrants )
            {
                lblTotalAmount.Text = String.Format( TOTAL_AMOUNT_FORMAT, this.TotalAmount() );
            }
        }
        #endregion

        #region Properties
        public event GrantSelectedEventHandler GrantSelectedListeners
        {
            add
            {
                this.GrantSelectedEvent += value;
            }
            remove
            {
                this.GrantSelectedEvent -= value;
            }
        }

        public event AddNewGrantEventHandler AddNewGrantListeners
        {
            add
            {
                this.AddNewGrantEvent += value;
            }
            remove
            {
                this.AddNewGrantEvent -= value;
            }
        }

        public Button AddGrantButton
        {
            get
            {
                return cmdAddGrant;
            }
        }

        #endregion

        #region Private Methods
        private Grant GrantWith( long oid )
        {
            return this.Grants.Find( grant => grant.Oid == oid );
        }
        private void RaiseGrantSelectedEvent( Grant grant )
        {
            if ( this.GrantSelectedEvent != null )
            {
                this.GrantSelectedEvent( grant );
            }
        }
        private List<Grant> SortedGrantList()
        {
            this.Grants.Sort( new GenericSorter.Sorter<Grant>( this.SortDescription ) );
            return this.Grants;
        }
        private decimal TotalAmount()
        {
            decimal total = 0;
            this.Grants.ForEach( delegate( Grant grant )
            {
                total += grant.Value;
            } );
            return total;
        }
        #endregion

        #region Private Properties
        private List<Grant> Grants
        {
            get
            {
                return (List<Grant>)this.Model;
            }
        }

        private bool HasGrants
        {
            get
            {
                return this.Grants != null && this.Grants.Count > 0;
            }
        }
        private SortDescription SortDescription
        {
            get
            {
                return (SortDescription)this.Session[SESSION_SORT_DESCRIPTION];
            }
            set
            {
                this.Session[SESSION_SORT_DESCRIPTION] = value;
            }
        }
        private GrantSelectedEventHandler GrantSelectedEvent
        {
            get
            {
                return i_GrantSelectedEvent;
            }
            set
            {
                i_GrantSelectedEvent = value;
            }
        }

        private AddNewGrantEventHandler AddNewGrantEvent
        {
            get
            {
                return i_AddNewGrantEvent;
            }
            set
            {
                i_AddNewGrantEvent = value;
            }
        }

        private void RaiseAddNewGrantEvent(Case aCase)
        {
            if (this.AddNewGrantEvent != null)
            {
                this.AddNewGrantEvent(aCase);
            }
        }

        private Case Case
        {
            get
            {
                CaseVariable variable = new CaseVariable();
                return variable.Case;
            }
        }

        #endregion

        #region Construction and Finalization
        #endregion

        #region Data Elements
        private GrantSelectedEventHandler i_GrantSelectedEvent;
        private AddNewGrantEventHandler i_AddNewGrantEvent;
        #endregion


        
}
}
